/**
 * Created by dasd on 2017/1/16.
 */
var route = function () {
    var pageHistory=[{url:'index.html'}];
    var current=0;
    var stackIn=function(url,title,params){
        var pageData={
            url:url,
            title:title,
            params:$.extend(true,{},params)
        };
        pageHistory[++current]=pageData;
    };
    var getCurrentPageName=function(){
        var url=pageHistory[current].url.replace(/\\/g,'/');
        var index=url.lastIndexOf('/')+1;
        return url.substring(index);
    };
    var goBackTo=function(urls){
        if(typeof urls==='string'){
            urls=urls.split(',');
        }
        if(!urls.length)return false;
        for(var i=current-1;i>=0;i--){
            var pageData=pageHistory[i],
                arr=pageData.url.split('/'),
                url=arr[arr.length-1];
            if(urls.indexOf(url)!=-1){
                current=i;
                loadPage(pageData.url,pageData.title,pageData.params,true);
                return true;
            }
        }
        return false;
    };
    //返回到之前的页面
    var goBack=function(urls){
        if(urls){
            return goBackTo(urls);
        }
        if(current==0){
            refleshPage();
            return;
        }
        var pageData=pageHistory[--current];
        loadPage(pageData.url,pageData.title,pageData.params,true);
    };
    var refleshPage=function(){

    };
    var loadPage =  function (url,title,params,isBack) {
        if(!url) return;
        lodingDate(url,null,isBack?null:function(){stackIn(url,title,params);}); //加载页面数据
    };
    return {
        loadPage:loadPage,
        getCurrentPageName:getCurrentPageName,
        goBack:goBack
    }
}();

